/**
 * Created by cj on 2017/5/7.
 */

// Define a simple Car constructor
var Car = function (settings) {
    this.model = settings.model || "no model provided";
    this.color = settings.color || "no colour provided";
};
// Mixin
var Mixin = function () {
};
Mixin.prototype = {
    driveForward: function () {
        console.log("drive forward");
    },
    driveBackward: function () {
        console.log("drive backward");
    },
    driveSideways: function () {
        console.log("drive sideways");
    }
};
// Extend an existing object with a method from another
function augment(receivingClass, givingClass) {
// only provide certain methods
    if (arguments[2]) {
        for (var i = 2, len = arguments.length; i < len; i++) {
            receivingClass.prototype[arguments[i]] = givingClass.prototype[arguments[i]];
        }
    }
// provide all methods
    else {
        for (var methodName in givingClass.prototype) {
// check to make sure the receiving class doesn't
// have a method of the same name as the one currently
// being processed
            if (!Object.hasOwnProperty(receivingClass.prototype, methodName)) {
                receivingClass.prototype[methodName] = givingClass.prototype[methodName];
            }
// Alternatively:
// if ( !receivingClass.prototype[methodName] ) {
// receivingClass.prototype[methodName] = givingClass.prototype[methodName];
// }
        }
    }
}
// Augment the Car constructor to include "driveForward" and "driveBackward"
augment(Car, Mixin, "driveForward", "driveBackward");
